﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using System.Collections;
using ExpertMIS.Entities;

namespace ExpertMIS.Admin
{
    public partial class ExpertList : ExpertMIS.Codes.AdminAuthenticationPage
    {
        private string MakeQueryCondition()
        {
            string strFilter = "";

//            if (!string.IsNullOrEmpty(txName.Text) && txName.Text.Trim().Length > 0)
//            {
//                strFilter = string.Format(" F_Name = '{0}'", txName.Text) ;
//            }

//            if (strFilter.Length > 0 && ddlDomain.SelectedItem.Text != "所有领域")
//            {
//                strFilter += string.Format(@" AND F_OID in 
//                    ( SELECT F_ExpertOID FROM T_EXP_ExpertBelongDomain 
//                      WHERE F_DomainOID = '{0}'", ddlDomain.SelectedValue);
//            }

            return strFilter;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pgvExpertList.ObjectName = "ExpertMIS.ExpertListView";
                RefreshDomain();
            }

            pgvExpertList.DeleteClientScript = "if(!confirm('您确定要删除该专家档案吗？')) return false; ";
            pgvExpertList.EditClientURL = "~/Admin/ExpertForm.aspx?oid={0}";
            pgvExpertList.DeleteClientURL = "~/Admin/ExpertList.aspx?oid={0}&action=delete";
            
            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                string oid = Request["oid"];
                switch (action)
                {
                    case "delete":
                        DeleteExpert(oid);
                        break;
                    default:
                        break;
                }
            }
        }

        protected void DeleteExpert(string OID)
        {
            try
            {
                ExpertBroker broker = new ExpertBroker();
                Hashtable hsQuery = new Hashtable();
                hsQuery.Add("OID", OID);
                ExpertEntity entity = broker.GetEntity(hsQuery) as ExpertEntity;

                if (entity == null)
                {
                    return;
                }
                broker.RemoveEntity(entity, null);
            }
            catch
            {
                return;
            }
        }


        protected void lbNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("ExpertForm.aspx", false);
            //pgvExpertList.FilterString = MakeQueryCondition();
        }

        private void RefreshDomain()
        {
            DomainBroker broker = new DomainBroker();
            IList<DomainEntity> list = broker.GetAllDomains();

            if (list != null && list.Count > 0)
            {
                DomainEntity entity = new DomainEntity();
                entity.OID = "all";
                entity.DomainName = "所有领域";
                list.Insert(0, entity);

                ddlDomain.DataSource = list;
                ddlDomain.DataTextField = "DomainName";
                ddlDomain.DataValueField = "OID";
                ddlDomain.DataBind();
            }
        }

        public string GetStartHiddenFieldClientID()
        {
            return this.pgvExpertList.GetStartHiddenFieldClientID();
        }
    }
}
